package com.qf.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.goods.entity.GoodsDetail;
import com.qf.goods.mapper.GoodsDetailMapper;
import com.qf.goods.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsDetailServiceImpl implements GoodsDetailService {

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(GoodsDetail goodsDetail) {
        int i = goodsDetailMapper.updateByPrimaryKeySelective(goodsDetail);
        if(i!=1){
            System.out.println("【修改商品信息】修改失败！");
            throw new RuntimeException("【修改商品信息】修改失败！");
        }
    }

    @Override
    public GoodsDetail findById(Long id) {
        return goodsDetailMapper.findById(id);
    }

    @Override
    public PageInfo<GoodsDetail> findBySortIdAndPage(Integer page, Integer rows, Long sortId) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<GoodsDetail> goodsList=goodsDetailMapper.findBySortId(sortId);

        //3、封装PageInfo
        PageInfo<GoodsDetail> pageInfo = new PageInfo<>(goodsList);

        //4、返回
        return pageInfo;
    }
}
